<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发布新投票</title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
	<script src="https://unpkg.com/vue@2.7.14/dist/vue.min.js"></script>
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
	<script src="js/vote.js"></script>
</head>
<body>
<div id="app">
	<app>
		<div id="voteManage" class="box">

		<h2>添加新投票</h2>
		<div class="content">
		<form method="post" action="view.html">
				<dl>
					<dt>投票内容：</dt>
					<dd>
					   <input type="hidden" name="entityId" value=""/>
					   <input type="text" class="input-text" v-model="vote.vname"  value=""/>
					</dd>
					<dt>投票类型：</dt>
					<dd>
					   <input type="radio" v-model="vote.vtype" value="单选" />单选
					   <input type="radio" v-model="vote.vtype" value="多选" />多选
					</dd>
					<dt>投票选项：</dt>

					<dd id="voteoptions">
						<p v-for="i in vote.items">
							<input type="text" class="input-text" v-model="i.iname" />
						</p>
					</dd>

					<dt></dt>
					<dd class="button">
						<input type="image" @click.prevent="save" src="images/button_submit.gif" />
						<a href="#" @click.prevent="add">增加选项</a>
						<a href="index.html">取消操作</a>
					</dd>
				</dl>
			</form>
		</div>
	</div>
	</app>
</div>
<script>
	var v = new Vue({
		el : "#app",
		data : {
			vote : {
				vname : "",
				vtype : "单选",
				items : [{iname:""}]
			}
		},
		methods:{
			add(){
				this.vote.items.push({iname:""});
			},
			save(){
				let p = new URLSearchParams();
				p.append("vname",this.vote.vname);
				p.append("vtype",this.vote.vtype);
				this.vote.items.forEach(i=>{
					p.append("iname",i.iname);
				})
				axios.post("saveVote",p).then(res=>{
					if(res.data.code){
						location.href = "index.html";
					} else {
						alert(res.data.msg);
					}
				})
			}
		}
	})
</script>
</body>
</html>

